﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SSN.DotNet;

namespace SSN.DotNextExtensionsTests
{
    [TestClass]
    public class Converters
    {
        [TestMethod]
        public void StringToBool()
        {
            Assert.IsTrue("y".ToBool());
            Assert.IsFalse("n".ToBool());
            Assert.IsTrue("yes".ToBool());
            Assert.IsFalse("no".ToBool());
            Assert.IsTrue("1".ToBool());
            Assert.IsTrue("true".ToBool());
            Assert.IsTrue("TRUE".ToBool());
            Assert.IsFalse("FALSE".ToBool());

            Assert.IsFalse("Cow".ToBool());
		  Assert.IsTrue("Cow".ToBool(true));
        }

        [TestMethod]
        public void BoolToString()
        {
            Assert.IsTrue(true.ToYesNo() == "Yes");
            Assert.IsTrue(true.ToYN() == "Y");
            Assert.IsTrue(false.ToYN() == "N");
            Assert.IsTrue(false.ToYesNo() == "No");
        }

        [TestMethod]
        public void StringToInt()
        {
            Assert.IsTrue("32".IsInt());
            Assert.IsTrue("-4".IsInt());
            Assert.IsFalse("93.23".IsInt());
            Assert.IsFalse("bark".IsInt());
            Assert.IsTrue("1".ToInt() == 1);

            bool exceptionCaught = false;
            int i = 0;
            try
            {
                i = "Cow".ToInt();
            }
            catch
            {
                exceptionCaught = true;
            }

            Assert.IsTrue(exceptionCaught);
            Assert.IsTrue(i == 0);

            exceptionCaught = false;

            try
            {
                i = "Cow".ToInt(4);
            }
            catch
            {
                exceptionCaught = true;
            }

            Assert.IsFalse(exceptionCaught);
            Assert.IsTrue(i == 4);


            i = "".ToInt();
            Assert.IsTrue(i == 0);
        }

        [TestMethod]
        public void StringToEnum()
        {
            Assert.IsTrue("Red".IsEnum<ColorTest>());
            Assert.IsFalse("orange".IsEnum<ColorTest>());
            Assert.IsTrue("bLuE".IsEnum<ColorTest>());

            Assert.IsTrue("gReEn".ToEnum<ColorTest>() == ColorTest.Green);
            bool catchException = false;

            try
            {
                "burp".ToEnum<ColorTest>();
            }
            catch
            {
                catchException = true;
            }

            Assert.IsTrue(catchException);
            catchException = false;
            ColorTest t = ColorTest.Blue;
            try
            {
                t = "burp".ToEnum<ColorTest>(ColorTest.Green);
            }
            catch
            {
                catchException = true;
            }

            Assert.IsFalse(catchException);
            Assert.IsTrue(t == ColorTest.Green);
        }

        public enum ColorTest
        {
            Red,
            Blue,
            Green
        }
    }
}
